The Files.exists
method has noticeably poor performance in JDK 8, and can slow an application significantly when used to check files
that don’t actually exist.
The same goes for Files.notExists
, Files.isDirectory
and Files.isRegularFile
from java.nio.file
package.
Note that this rule is automatically disabled when the project’s sonar.java.source
is not 8.
Noncompliant code example
Path myPath;
if(java.nio.file.Files.exists(myPath)) { // Noncompliant
// do something
}
Compliant solution
Path myPath;
if(myPath.toFile().exists())) {
// do something
}